package com.example.cinemasystemv2.service;

import com.example.cinemasystemv2.entity.Snack;
import com.example.cinemasystemv2.repository.SnackRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class SnackService {
    
    @Autowired
    private SnackRepository snackRepository;
    
    @Autowired
    private SystemLogService systemLogService;
    
    public List<Snack> getAllSnacks() {
        return snackRepository.findAll();
    }
    
    public Optional<Snack> getSnackById(Long id) {
        return snackRepository.findById(id);
    }
    
    public Optional<Snack> getSnackByName(String name) {
        return snackRepository.findByName(name);
    }
    
    public List<Snack> getAvailableSnacks() {
        return snackRepository.findAvailableSnacks();
    }
    
    public List<Snack> getLowStockSnacks(int threshold) {
        return snackRepository.findLowStockSnacks(threshold);
    }
    
    public List<Snack> searchSnacks(String keyword) {
        return snackRepository.searchSnacks(keyword);
    }
    
    public Snack saveSnack(Snack snack) {
        Snack savedSnack = snackRepository.save(snack);
        systemLogService.log("零食保存: " + snack.getName(), "system", "SAVE_SNACK");
        return savedSnack;
    }
    
    public Snack createSnack(String name, double price, int stock) {
        if (snackRepository.existsByName(name)) {
            throw new IllegalArgumentException("零食名称已存在");
        }
        
        Snack snack = new Snack(name, price, stock);
        return saveSnack(snack);
    }
    
    public Snack updateSnack(Snack snack) {
        Snack updatedSnack = snackRepository.save(snack);
        systemLogService.log("零食更新: " + snack.getName(), "system", "UPDATE_SNACK");
        return updatedSnack;
    }
    
    public void deleteSnack(Long id) {
        Optional<Snack> snackOpt = snackRepository.findById(id);
        if (snackOpt.isPresent()) {
            Snack snack = snackOpt.get();
            snackRepository.deleteById(id);
            systemLogService.log("零食删除: " + snack.getName(), "system", "DELETE_SNACK");
        }
    }
    
    public boolean sellSnack(Long snackId, int quantity, String username) {
        Optional<Snack> snackOpt = snackRepository.findById(snackId);
        if (snackOpt.isPresent()) {
            Snack snack = snackOpt.get();
            if (snack.getStock() >= quantity) {
                snack.reduceStock(quantity);
                snackRepository.save(snack);
                
                double totalAmount = snack.getPrice() * quantity;
                systemLogService.log("零食销售: " + snack.getName() + " x" + quantity + 
                                   " - 金额: " + totalAmount, username, "SELL_SNACK");
                return true;
            } else {
                throw new IllegalArgumentException("库存不足");
            }
        }
        return false;
    }
    
    public void addStock(Long snackId, int quantity) {
        Optional<Snack> snackOpt = snackRepository.findById(snackId);
        if (snackOpt.isPresent()) {
            Snack snack = snackOpt.get();
            snack.addStock(quantity);
            snackRepository.save(snack);
            systemLogService.log("零食补货: " + snack.getName() + " +" + quantity, "system", "ADD_STOCK");
        }
    }
    
    public boolean existsByName(String name) {
        return snackRepository.existsByName(name);
    }
    
    public long getAvailableSnacksCount() {
        return snackRepository.countAvailableSnacks();
    }
    
    public void initializeDefaultSnacks() {
        if (snackRepository.count() == 0) {
            createSnack("爆米花(大)", 25.0, 100);
            createSnack("爆米花(中)", 20.0, 100);
            createSnack("爆米花(小)", 15.0, 100);
            createSnack("可乐(大)", 12.0, 50);
            createSnack("可乐(中)", 10.0, 50);
            createSnack("雪碧(大)", 12.0, 50);
            createSnack("薯片", 8.0, 80);
            createSnack("巧克力", 15.0, 60);
            systemLogService.log("初始化默认零食", "system", "INIT_SNACKS");
        }
    }
}